package com.jack.helloworld.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.jack.helloworld.R;
import com.jack.helloworld.fragment.DynamicFragment;

public class FragmentMainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnStatic;
    Button btnDynamic;
    Button btnToFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_main);

        initView();
        initData();

    }

    private void initView() {
        btnStatic = (Button) findViewById(R.id.btnStatic);
        btnDynamic = (Button) findViewById(R.id.btnDynamic);
        btnToFragment = (Button) findViewById(R.id.btnToFragment);
    }

    private void initData() {
        btnStatic.setOnClickListener(this);
        btnDynamic.setOnClickListener(this);
        btnToFragment.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStatic:
                Intent intent = new Intent(this, FragmentTestActivity.class);
                startActivity(intent);
                break;
            case R.id.btnDynamic:
                Fragment fragment = new DynamicFragment();
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.llFragment, fragment);
                fragmentTransaction.addToBackStack(null);  //回到初始状态
                fragmentTransaction.commit();

                break;
            case R.id.btnToFragment:
                intent = new Intent(this, FragmentReceiveActivity.class);
                startActivity(intent);
                break;
        }

    }
}
